package com.itheima.controller;

import com.itheima.domain.Book;
import com.itheima.server.impl.BookImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/books")
public class WebController {
    @Autowired
    private Result result;

    @Autowired
    private BookImpl bookImpl;

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        return new Result(bookImpl.findById(id), Code.GET_OK, "获取成功");
    }

    @GetMapping
    public Result findAll() {
        return new Result(bookImpl.serchList(), Code.GET_OK, "获取成功");
    }

    @PostMapping
    public Result insert(@RequestBody Book book) {
        return new Result(bookImpl.save(book), Code.SAVE_OK, "添加成功");
    }
}
